home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power Programmierung
/
Power-Programmierung (Tewi)(1994).iso
/
mutt
/
isearch.mut
< prev
next >
Wrap
Lisp/Scheme
|
1988-09-08
|
4KB
|
145 lines
;; isearch.mut: Incremental searching.
;; Searching as you type - shows you where the string you have typed so
;; far would be found.
;; Keys:
;; ^H (Backspace): Remove the last character from the search pattern.
;; Don't move.
;; ^S: Search forward for existing pattern. If haven't entered a
;; pattern, the old pattern is used.
;; ^R: Search reverse for pattern.
;; ^Q: Quote the next character into the search pattern. Usefull for
;; searching for control characters.
;; ^W: Add the rest of the word to the search pattern.
;; ^G: Exit search.
;; ^M (Enter): Drop to non incremental search if haven't done anything
;; yet.
;; Control characters and combo keys (eg cursor motion, C-X=, etc)
;; terminate the search. The key is then executed. For example,
;; searching for text and then pressing C-P will cause the cursor to
;; move to the line above the matched text.
(defun chr$ (int n)(array byte str 2) HIDDEN { (str 0 n)(str 1 0) str })
(string oldpat 80)
(defun isearch (bool forward-search) HIDDEN
{
(string ipat 80 hoho 40 tmp 3 direction 10)
(int sc len)
(bool firsttime forward)
(direction (if (forward forward-search) "forward" "reverse"))
(firsttime TRUE)(ipat (hoho ""))
(while TRUE
{
(msg "I-search-" direction " [" ipat "] " hoho ' (^S ^R ^W ^Q)')
(if (not (key-waiting)) (update))
(hoho "")
(switch (sc (get-key))
0x148 ; backspace
(ipat (substr ipat 0 -1))
0x153 ; ^S
{
(forward TRUE)(direction "forward")
(if firsttime
{
(firsttime FALSE)
(if (== oldpat "")(continue))
(ipat oldpat)
})
(if (not (search-forward ipat))(hoho "Not found."))
}
0x152 ; ^R: reverse the search
{
(forward FALSE)(direction "reverse")
(if firsttime
{
(firsttime FALSE)
(if (== oldpat "")(continue))
(ipat oldpat)
(if (search-reverse ipat)
(search-forward "") ; put dot at end of pattern
(hoho "Not found.")
)
}
{
(arg-prefix (strlen ipat))(previous-character)
(if (search-reverse ipat)
(search-forward "") ; put dot at end of pattern
{
(arg-prefix (strlen ipat))(next-character)
(hoho "Not found.")
}
)
}
)
}
0x151 ; ^Q: quote next character
{
(len (strlen ipat))
(ipat (concat ipat (getchar)))
(goto ack)
}
0x157 ; ^W: grab word
{
(if (looking-at '\w+')
{
(len (strlen ipat))
(ipat (concat ipat (get-matched '&')))
(goto ack) ; we know the search will succeed
})
(hoho "Not looking at a word.")
}
0x14D ; ^M: drop down into non incremental search
{
(if firsttime
{
(ask-user)
(if forward (search-forward) (search-reverse))
(done)
})
}
0x147 ; ^G: quit
{
(msg "Done.")
(label quit)
(if (!= "" ipat) (oldpat ipat)) ; save search pattern (if interesting).
(done)
}
default
{
(if (> sc 0xFF) { (msg "")(exe-key sc)(goto quit) })
(len (strlen ipat))
(ipat (concat ipat (chr$ sc tmp)))
(label ack)
(firsttime FALSE)
(if forward
{
(arg-prefix len)(previous-character) ; move to start of pattern
(if (not (search-forward ipat))
{
(arg-prefix len)(next-character)
(label gack)
(ipat (substr ipat 0 -1))(hoho "Not found.")
})
}
{
(if (search-reverse ipat)
(search-forward "")
(goto gack)
)
}
)
}
)
})
})
(defun
isearch-forward { (isearch TRUE) } ; Normally bound to C-S
isearch-reverse { (isearch FALSE) } ; Normally bound to C-R
)